<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<div class="header box">
<%--
	<div id="tools_guest" class="login">
		<a href="${ctx}/reg/reg.do">注册</a>
		<a href="${ctx}/login/index.do">登录</a>
	</div>
   	<div id="tools_user" style="display: none;" class="login">
   		<c:if test="${user.status==400}">
   		<a href="${ctx}/manager/apply.do">管理</a>
   		</c:if>
		<a href="javascript:void(0)" onclick="logout()">注销</a>
	    <a id="head_msg_show" href="${ctx}/frame/order.do" >新消息(0)</a>
   		<a id="header_tools_user_hello" href="${ctx}/frame/order.do"></a>
   	</div>
   	--%>
   	
   	<div class="nav">
		<div class="logo"><a href="${ctx}/"></a></div>
        <div class="nav_side" id="tools_guest" <c:if test="${user != null}">style="display: none;"</c:if>>
	        <a class="nav_short_btn"  href="${ctx}/login/index.do"><img src="${ctx}/images/btn_icon_login.png" />登录</a>
	        <a class="nav_short_btn" href="${ctx}/reg/reg.do"><img src="${ctx}/images/btn_icon_reg.png" />注册</a>
	        <a href="${ctx}/host/apply.do"><img src="${ctx}/images/btn_icon_aply.png" />申请代养资格</a>
        </div>
        <div class="nav_side" id="tools_user" <c:if test="${user == null}">style="display: none;"</c:if>>
	        <img src="${ctx}/images/btn_icon_login.png" /><span id="header_tools_user_hello" style="cursor:pointer;" onclick="window.location.href='${ctx}/frame/order.do'">${user.userNickName}</span>
	        &nbsp;&nbsp;&nbsp;&nbsp;<span id="head_msg_show" style="cursor:pointer;" onclick="tools.href('${ctx}/frame/msg.do')" >新消息(0)</span>
	        <a class="nav_short_btn" href="javascript:void(0)" onclick="logout()"><img src="${ctx}/images/btn_icon_logout.png" />注销</a>
	        <c:choose>
			   <c:when test="${user != null && user.hostApply != null && user.hostApply.status == 100}">
					<img src="${ctx}/images/btn_icon_aply.png" />资格审核中
			   </c:when>
			   <c:when test="${user != null && user.host != null}">
					
			   </c:when>
			   <c:otherwise>
					<a href="${ctx}/host/apply.do"><img src="${ctx}/images/btn_icon_aply.png" />申请代养资格</a>
			   </c:otherwise>
			</c:choose>
	        <c:if test="${user.status==400}">
	   		<span onclick="tools.href('${ctx}/manager/apply.do')">管理</span>
	   		</c:if>
        </div>
    </div>
   	
	<script type="text/javascript">
	var unreadMsgLock = false;
		<c:if test="${user != null}">
		unreadMsgLock = true;
		</c:if>
	//刷新消息数量
	var refreshMsg = function() {
		if(!unreadMsgLock) return;
		$.ajax({
			url:"${ctx}/msg/unread.do",
			cache:false,
			contentType:"application/x-www-form-urlencoded;charset=utf-8",
			success:function(data) {
				if($.isNumeric(data)) {
					if(data > 0){
						$("#head_msg_show").css("color","red");
					} else {
						$("#head_msg_show").css("color","");
					}
					$("#head_msg_show").html("新消息(" + data + ")");
				}
			},
			complete:function(XMLHttpRequest, textStatus) {
				var sessionstatus = XMLHttpRequest.getResponseHeader("SessionStatus");
				if(sessionstatus=="timeout") {
					unreadMsgLock = false;
					$("#tools_guest").show();
					$("#tools_user").hide();
				}
			}
		});
	};
	
	//退出登录
	var logout = function() {
		$.get("${ctx}/login/logout.do", function() {
			$("#tools_guest").show();
			$("#tools_user").hide();
			unreadMsgLock = false;
		});
	};
	
	$(function() {
		//定时刷新消息
		refreshMsg();
		setInterval(refreshMsg, 1000*30);
		//判断用户是否登录
// 		$.getJSON("${ctx}/login/issign.do", function(res) {
// 			if(res.code == 1) {
// 				var showName = res.attributes.user.userNickName;
// 				unreadMsgLock = true;
// 				$("#tools_guest").hide();
// 				$("#header_tools_user_hello").html(showName);
// 				$("#tools_user").show();
// 				refreshMsg();
// 			} else {
// 				$("#tools_guest").show();
// 				$("#tools_user").hide();
// 			}
// 		});		
	});
	</script>
    
</div>